package gui.listeners;

import gui.MainWindow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public class MainWindowMenuListener implements ActionListener{
	
	public static final String ACTION_LOAD_PHOTOS = "loadPhotos";
	
  private MainWindow window;
	
	public MainWindowMenuListener(MainWindow window){
		this.window = window;
	}

	@Override
  public void actionPerformed(ActionEvent e) {
	  if(e.getActionCommand().equals(ACTION_LOAD_PHOTOS)){
	  	JFileChooser fileChooser = new JFileChooser();
	  	fileChooser.setDialogTitle("Add Photos...");
	  	fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
	  	fileChooser.setMultiSelectionEnabled(true);
	  	fileChooser.setFileFilter(new FileFilter() {
				@Override
				public String getDescription() {
					return "JPEG Photo (*.jpg, *.jpeg)";
				}
				
				@Override
				public boolean accept(File f) {
					if(f.isDirectory()){
						return true;
					}
					if(f.getAbsolutePath().toLowerCase().endsWith(".jpeg") || 
						 f.getAbsolutePath().toLowerCase().endsWith(".jpg")){
						return true;
					}
					return false;
				}
			});
	  	
	  	int returnValue = fileChooser.showOpenDialog(window);
	  	if(returnValue == JFileChooser.APPROVE_OPTION){
	  		window.getCurrentEditor().loadFiles(fileChooser.getSelectedFiles());
	  	}
	  }
	}
}
